<?php
/**
 * @Name: LoginLogic.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace App\Http\Logic\Rbac;

use App\Http\Logic\BaseLogic;
use App\Http\Model\Rbac\UserModel;
use HugCode\PhpUnits\Frame\Code\ConstCode;
use HugCode\PhpUnits\CipherText\Password;

class LoginLogic extends BaseLogic
{

    /**
     * @Desc 密码登录
     * @return array
     * @throws \App\Exceptions\ThrowException
     * @author yashuai<1762910894@qq.com>
     */
    public function passwordAuth()
    {
        $info = UserModel::getInfo($this->params['UserName'], 'UserName');
        if (empty($info)) {
            _throw('', 107001);
        }
        if ($info['UserStatus'] != ConstCode::BASIC_STATUS_NORMAL) {
            _throw('', 107003);
        }
        if (empty($info['UserPassword']) || !Password::validatePassword($this->params['Password'], $info['UserPassword'])) {
            _throw('', 107002);
        }
        return $this->authAfter($info);
    }

    /**
     * @Desc 验证码登录
     * @author yashuai<1762910894@qq.com>
     */
    public function phoneCodeAuth()
    {
        $user = [];
        return $this->authAfter($user);
    }

    /**
     * @Desc 用户认证之后
     * @param array $userInfo
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    private function authAfter(array $userInfo = [])
    {
        return [
            'UserID'    => $userInfo['UserID'],
            'UserName'  => $userInfo['UserName'],
            'NickName'  => $userInfo['NickName'],
            'UserEmail' => $userInfo['UserEmail'],
            'Token'     => UserTokenLogic::instance()->createToken($userInfo['UserID'])
        ];
    }

}
